<?

/**
 * PhpMailer Builder class
 * @package simplemailer
 * @author Alberto Pardo Saleme <apardo@gmail.com>
 * @update 22/08/2007 01:48:25 p.m.
 * Copyright (C) 2005 - AdVerit S.A. <www.adverit.com>
 */
require_once(dirname(__FILE__) ."/SimpleMailerEngine.php");

class SimpleMailerSwift extends SimpleMailerEngine
{
    function send()
    {
        //require_once(dirname(__FILE__) ."/../lib/Swift/swift_required.php");
        require_once(dirname(__FILE__) ."/../lib/Swift/Swift.php");
        require_once(dirname(__FILE__) ."/../lib/Swift/Swift/Connection/SMTP.php");
        try
        {
            // Preparing MIME message
            $message =& new Swift_Message($this->subject, $this->body, "text/html");
            if(count($this->arAttachments))
            {
                $message->attach(new Swift_Message_Part($this->body, "text/html"));
                foreach($this->arAttachments as $attachment)
                {
                    $message->attach(new Swift_Message_Attachment(new Swift_File($attachment['path']), $attachment['name']));
                }
            }

            // Preparing recipients
            $swiftRecipients =& new Swift_RecipientList();
            $recipients = $this->parseEmailsList($this->to);
            foreach($recipients AS $recipient)
            {
                $recipient[1]? $swiftRecipients->addTo($recipient[0], $recipient[1]) : $swiftRecipients->addTo($recipient[0]);
            }

            // Preparing From
            list($from, $fromEmail) = $this->parseEmail($this->from);
            $swiftFrom = $fromEmail? new Swift_Address($from, $fromEmail) : new Swift_Address($from);

            // Connecting to MTA
            if ($GLOBALS["CONF"]['mail']['ssl'])
            {
                $smtp =& new Swift_Connection_SMTP($GLOBALS["CONF"]['mail']['host'], Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
            }
            else
            {
                $smtp =& new Swift_Connection_SMTP($GLOBALS["CONF"]['mail']['host']);
            }
            $smtp->setUsername($GLOBALS["CONF"]['mail']['user']);
            $smtp->setPassword($GLOBALS["CONF"]['mail']['pass']);

            // Delivery
            $swift =& new Swift($smtp);
            $success = $swift->send($message, $swiftRecipients, $swiftFrom);

            //It's polite to do this when you're finished
            $swift->disconnect();
        }
        catch (Swift_ConnectionException $e)
        {
            $this->_error("There was a problem communicating with SMTP: " . $e->getMessage());
        }
        catch (Swift_Message_MimeException $e)
        {
            $this->_error("There was an unexpected problem building the email:" . $e->getMessage());
        }
        return $success? 1 : 0;
    }
}

?>
